/**
 * @Copyright: Interactive Business Solution Pte Ltd, Singapore
 * @Author: Dolita, 2007-12-24
 * @Version: 071224.001
 * @Change: Li Wen, 2008-03-21
 * Data access object of CityMap model
 */
package com.ibsol.database
{
	import com.ibsol.model.CityMap;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	public class CityMapDAO
	{
		private var _tourixDBConn:SQLConnection;
		/**
		 * Default constructor of CityMap DAO, get database connection
		 */
		public function CityMapDAO()
		{
			_tourixDBConn = DBConnManager.getConnection("tourix");
		}
		/**
		 * Insert data into cityMap table
		 * cityMap(id, packageid, name, scale, initX, initY, offlineOperator, updateTime)
		 * @param: cityMap:CityMap			cityMap item object which will be populating cityMap table
		 * 		   resultHandler:Function	result function adapter
		 * 		   faultHandler:Function	fault function adapter
		 * @return: void
		 */
		public function insertCityMap(cityMap:CityMap, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "INSERT INTO cityMap (packageid, name, scale, initX, initY, offlineOperator, updateTime) " + 
						"VALUES (" + cityMap.packageid + ",'" + cityMap.name + "', '" + cityMap.scale + "', " + cityMap.initX + 
						", " + cityMap.initY + ", '" + cityMap.offlineOperator + "', 'T')";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					cityMap.id = sqlStr.getResult().lastInsertRowID;
					if(resultHandler != null)
						resultHandler.call(this, cityMap.id);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}						
		/**
		 * Load city map according to package id
		 * @param:	packageid:int			package id which the city map belongs to
		 * 			resultHandler:Function	result function adapter, return cityMap array collection
		 * 			faultHandler:Function	fault function adapter
		 * @return: void
		 */
		public function loadCityMapsByPackage(packageid:int, resultHandler:Function, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "SELECT * FROM cityMap WHERE packageid = '" + packageid + "' AND offlineOperator != 'DEL'";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					resultHandler.call(this, typeArray(sqlStr.getResult().data));
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Updates a cityMap record in database. For different level of user, there are different options.
		 * cityMap(id, packageid, name, scale, initX, initY, offlineOperator, updateTime)
		 * @param:	cityMap:CityMap			cityMap object which will be updated
		 * 			resultHandler:Function	result handler function
		 * 			faultHandler:Function	fault handler function
		 * @return: void
		 */	
		public function updateCityMap(cityMap:CityMap, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE cityMap SET packageid=" + cityMap.packageid + ",name='" + cityMap.name + "',scale='" + cityMap.scale + 
					"', initX=" + cityMap.initX + ", initY=" + cityMap.initY + ",offlineOperator = '" + cityMap.offlineOperator + 
					"',updateTime='T' WHERE id = " + cityMap.id;
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Permanently delete cityMap from database, whose offlineOperator is set to "DEL". If user wants to delete city, 
		 * user just updates offlineOperator to "DEL".
		 * @param:	resultHandler:Function		result handler adapter
		 * 			faultHandler:Function		fault handler adapter
		 * @return:	void
		 */
		public function clearDeletedCityMaps(resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "DELETE FROM cityMap WHERE offlineOperator = 'DEL'";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Sets offline operator to "", this function called after synchronization with server
		 * @param:	resultHandler:Function		result handler adapter
		 * 			faultHandler:Function		fault handler adapter
		 * @return: void
		 */
		public function resetOfflineOperator(resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE cityMap SET offlineOperator=''";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function (event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Converts database object to CityMap class
		 * @param:	o:Object	database return object
		 * @return:	CityMap		CityMap object after conversion
		 */
		private function typeObject(o:Object):CityMap
		{ 
			var cm:CityMap = new CityMap();
			cm.id = o.id;
			cm.packageid = o.packageid;
			cm.name = o.name;
			cm.scale = o.scale;
			cm.initX = o.initX;
			cm.initY = o.initY;
			cm.offlineOperator = o.offlineOperator;
			cm.updateTime = o.updateTime;
			
			return cm;
		}
		/**
		 * Converts array object to CityMap array collection
		 * @param: a:Array array of objects returned by SQL
		 * @return: ArrayCollection of CityMap after conversion from Array
		 */
		private function typeArray(a:Array):ArrayCollection
		{
			if (a==null) return null;
			var ac:ArrayCollection = new ArrayCollection();
			for (var i:int; i<a.length; i++)
			{
				ac.addItem(typeObject(a[i]));
			}
			return ac;
		}
		/**
		 * Default SQL error handler, if above functions' fault handlers are null, this error handler is applied
		 * @param: event:SQLError	SQL Error event will be handled if there is an error
		 * @return: void
		 */
		private function sqlErrorHandler(event:SQLErrorEvent):void
		{
			mx.controls.Alert.show(event.text, "Error");
			trace(event.text);
		}
	}
}